home *** CD-ROM | disk | FTP | other *** search
/ MacFormat 1997 July / macformat52.iso / mac / Shareware Plus / Developers / YAAF v1.0 alpha 1 / Headers / Core / XDynArray.h < prev    next >
Encoding:
C/C++ Source or Header  |  1997-04-24  |  4.6 KB  |  186 lines

  1. /*    XDynArray.h
  2.  *
  3.  *        This is my custom classes for manipulating a dynamic array
  4.  *    of objects. As this is pretty common, yet the 'dynarray' standard
  5.  *    C++ class is not declared for either VC++, Metrowerks C++, or
  6.  *    the GNU C++ library, I'm doing the minimum functionality I need here.
  7.  */
  8.  
  9. /*  YAAF - Yet another application framework
  10.  *  Copyright (C) 1997 William Edward Woody and In Phase Consulting
  11.  *  
  12.  *  This library is free software; you can redistribute it
  13.  *  and/or modify it under the terms of the GNU Library
  14.  *  General Public License as published by the Free Software
  15.  *  Foundation; either version 2 of the License, or any
  16.  *  later version.
  17.  *  
  18.  *  This library is distributed in the hope that it will be
  19.  *  useful, but WITHOUT ANY WARRANTY; without even the implied
  20.  *  warranty of MERCHANTABIILITY or FITNESS FOR A PARTICULAR
  21.  *  PURPOSE. See the GNU Library General Public License for
  22.  *  more details.
  23.  *  
  24.  *  You should have received a copy of the GNU Library General
  25.  *  Public License along with this library; if not, write to the
  26.  *  Free Software Foundation, Inc., 59 Temple Place - Suite 330,
  27.  *  Boston, MA 02111-1307, USA.
  28.  *  
  29.  *  To contact the author, either e-mail me at
  30.  *  woody@alumni.caltech.edu, or write to us at
  31.  *  
  32.  *          William Edward Woody
  33.  *          In Phase Consulting
  34.  *          1545 Ard Eevin Avenue
  35.  *          Glendale, CA 91202
  36.  */
  37.  
  38. #ifndef __XDYNARRAY_H__
  39. #define __XDYNARRAY_H__
  40.  
  41. #if defined(__MWERKS__)
  42.     #if defined(macintosh)
  43.         #pragma options align=power
  44.     #endif
  45.     #if defined(__INTEL__)
  46.         #pragma pack(push,2)
  47.     #endif
  48. #endif
  49.  
  50. /************************************************************************/
  51. /*                                                                        */
  52. /*    Class declarations                                                    */
  53. /*                                                                        */
  54. /************************************************************************/
  55.  
  56. /*    XGDynArray
  57.  *
  58.  *        This implements enough of a dynamic array template for me.
  59.  */
  60.  
  61. template <class T> class XGDynArray {
  62.     public:
  63.                                 XGDynArray()
  64.                                     {
  65.                                         fAlloc = 0;
  66.                                         fLength = 0;
  67.                                         fArray = NULL;
  68.                                     }
  69.         virtual                    ~XGDynArray()
  70.                                     {
  71.                                         if (fArray) delete[] fArray;
  72.                                     }
  73.         
  74.         /*
  75.          *    Content manipulation
  76.          */
  77.         
  78.         void                    Insert(long i,const T& t)
  79.                                     {
  80.                                         T *array;
  81.                                         long j;
  82.                                         
  83.                                         /* Range check */
  84.                                         if (i < 0) throw "Out of range";
  85.                                         if (i > fLength) throw "Out of range";
  86.                                         
  87.                                         if (fArray == NULL) {
  88.                                             /* Allocate new pointer fresh */
  89.                                             fAlloc = 16;
  90.                                             fLength = 1;
  91.                                             fArray = new T[fAlloc];
  92.                                             fArray[i] = t;
  93.                                         } else {
  94.                                             if (fLength >= fAlloc) {
  95.                                                 /* Grow allocation */
  96.                                                 array = new T[16+fAlloc];
  97.                                                 fAlloc += 16;
  98.                                                 
  99.                                                 /* Copy to new array */
  100.                                                 for (j = 0; j < i; j++) {
  101.                                                     array[j] = fArray[j];
  102.                                                 }
  103.                                                 array[i] = t;
  104.                                                 for (j = i; j < fLength; j++) {
  105.                                                     array[j+1] = fArray[j];
  106.                                                 }
  107.                                                 fLength++;
  108.                                                 
  109.                                                 /* Replace old array */
  110.                                                 delete[] fArray;
  111.                                                 fArray = array;
  112.                                             } else {
  113.                                                 /* Move array around */
  114.                                                 for (j = fLength; j > i; i--) {
  115.                                                     fArray[j] = fArray[j-1];
  116.                                                 }
  117.                                                 fArray[i] = t;
  118.                                                 fLength++;
  119.                                             }
  120.                                         }
  121.                                     }
  122.         
  123.         void                    Delete(long i)
  124.                                     {
  125.                                         T *array;
  126.                                         long j;
  127.                                         
  128.                                         /* Range check */
  129.                                         if (i < 0) throw "Out of range";
  130.                                         if (i > fLength) throw "Out of range";
  131.                                         
  132.                                         /* Move objects over */
  133.                                         for (j = i+1; j < fLength; j++) {
  134.                                             fArray[j-1] = fArray[j];
  135.                                         }
  136.                                         fLength--;
  137.                                         
  138.                                         /* Shrink allocation (if needed) */
  139.                                         j = fLength;
  140.                                         if (j % 16) j += 16 - (j % 16);
  141.                                         if (j == 0) j = 16;
  142.                                         if (j != fAlloc) {
  143.                                             /* Resize this thing */
  144.                                             array = new T[j];
  145.                                             if (array) {
  146.                                                 for (j = 0; j < fLength; j++) {
  147.                                                     array[j] = fArray[j];
  148.                                                 }
  149.                                                 delete[] fArray;
  150.                                                 fArray = array;
  151.                                             }
  152.                                         }
  153.                                     }
  154.         
  155.         long                    Length() const
  156.                                     {
  157.                                         return fLength;
  158.                                     }
  159.         
  160.         XGDynArray<T> &            operator += (const T&t)
  161.                                     {
  162.                                         Insert(fLength,t);
  163.                                         return *this;
  164.                                     }
  165.         
  166.         T &                        operator [](long l)
  167.                                     {
  168.                                         return fArray[l];
  169.                                     }
  170.     private:
  171.         long                    fLength;
  172.         long                    fAlloc;
  173.         T                        *fArray;
  174. };
  175.  
  176. #if defined(__MWERKS__)
  177.     #if defined(macintosh)
  178.         #pragma options align=reset
  179.     #endif
  180.     #if defined(__INTEL__)
  181.         #pragma pack(pop)
  182.     #endif
  183. #endif
  184.  
  185. #endif
  186.